#include "editfolderproperties_dialog.h"
#include "ui_editfolderproperties_dialog.h"
#include <QSettings>

editFolderProperties_dialog::editFolderProperties_dialog(Folder *pFolder, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::editFolderProperties_dialog)
{    
    ui->setupUi(this);

    curFolder = pFolder;

    setWindowTitle( windowTitle() + curFolder->getName());
    // Вывод информации о папке
    ui->iconBox->setCurrentIndex( curFolder->getIconStyle());   // Иконка
    ui->commentaryField->setText( curFolder->getCommentary());  // Комментарий
    ui->tagList->addItems(curFolder->getTags());                // Теги

    for( int i = 0; i < ui->tagList->count(); i++)
    {
        ui->tagList->item(i)->setBackgroundColor(QColor("#B8E5FF"));
        ui->tagList->item(i)->setIcon(QIcon(":/icons/tag_icon.png"));
    }
}

void editFolderProperties_dialog::on_saveButton_pressed()
{
    curFolder->setIconStyle( ui->iconBox->currentIndex() );
    curFolder->setCommentary( ui->commentaryField->text());

    curFolder->clearTags();

    for( int i = 0; i < ui->tagList->count(); i++)
        curFolder->addTag( ui->tagList->item(i)->text());

    /* ---- Получение списка тегов из реестра и добавление к ним отсутствующих--- */
    QStringList *tagList = new QStringList;
    QSettings *settings  = new QSettings("Cydeamon's Projects", "Gameloc");

    *tagList = settings->value("tags").toStringList();

    bool alreadyHave;
    for( int i = 0; i < curFolder->getTags().count(); i++)
    {
        alreadyHave = false;

        for( int j = 0; j < tagList->count(); j++)
            if( tagList->at(j) == curFolder->getTags().at(i))
                alreadyHave = true;

        if( !alreadyHave )
            tagList->push_back(curFolder->getTags().at(i));
    }

    settings->setValue("tags", *tagList);

    if( ui->addTagsToSounds->isChecked())
        for( int i = 0; i < curFolder->getFilesCount(); i++)
            for( int j = 0; j < curFolder->getTags().count(); j++)
                curFolder->getFile(i)->addTag(curFolder->getTags().at(j));

    delete settings;
    delete tagList;

    emit saved(curFolder);

    close();
    deleteLater();
}

void editFolderProperties_dialog::on_plusButton_pressed()
{
    addTagDialog = new addTag_dialog(this);
    addTagDialog->show();

    connect( addTagDialog, SIGNAL(tagReady()), SLOT(getTag()));
}

void editFolderProperties_dialog::on_minusButton_pressed()
{
    QListWidgetItem *itemToDelete = ui->tagList->currentItem();

    itemToDelete->setHidden(true);
    delete itemToDelete;

    if( ui->tagList->count() == 0)
        ui->minusButton->setEnabled(false);
}

void editFolderProperties_dialog::getTag()
{
    bool alreadyHave = false;

    for( int i = 0; i < ui->tagList->count(); i++)
        if( ui->tagList->item(i)->text() == addTagDialog->getTag())
            alreadyHave = true;

    if( alreadyHave )
        QMessageBox::information(this, "Тег уже есть", tr("Тег \"%1\" уже есть в списке").arg((addTagDialog->getTag())));
    else
    {
        ui->tagList->addItem( addTagDialog->getTag());
        ui->tagList->item( ui->tagList->count() - 1)->setBackgroundColor(QColor("#B8E5FF"));
        ui->tagList->item( ui->tagList->count() - 1)->setIcon(QIcon(":/icons/tag_icon.png"));
        ui->tagList->setCurrentItem( ui->tagList->item(ui->tagList->count() - 1));

        ui->minusButton->setEnabled(true);
    }
    addTagDialog->close();
    delete addTagDialog;
}

void editFolderProperties_dialog::on_cancelButton_pressed()
{    
    close();
    deleteLater();
}

editFolderProperties_dialog::~editFolderProperties_dialog()
{
    delete ui;
}
